<template>
  <u-upload
      :fileList="fileList1"
      @afterRead="afterRead"
      @delete="deletePic"
      name="1"
      multiple
      :maxCount="limit"
      :width="size"
      :height="size"
  ></u-upload>
</template>

<script>
import config from '../../../config/config'

export default {
  props: {
    limit: {
      type: Number,
      default: 10
    },
    size: {
      type: Number,
      default: 80
    },
    fileList1: {
      type: Array,
      default: []
    }
  },
  watch: {
    /*fileList1: {
      deep: true,
      handler(v) {
        // console.log(v)
      }
    }*/
    // list: {
    //   deep: true,
    //   handler(v) {
    //     console.log(v)
    //   }
    // }
  },
  created() {
    // console.log(this.fileList1)
    // console.log(this.list)
    // const list = []
    // if (this.list.length > 0) {
    //   this.list.forEach(x => {
    //     this.$set(x, 'url', config.baseUrl + x.path)
    //   })
    //   this.fileList1 = [...this.list]
    // }
    // console.log(this.fileList1)
    if (this.fileList1.length > 0) {
      this.fileList1.forEach(x => {
        this.$set(x, 'url', config.baseUrl + x.path)
      })
    }
  },
  data() {
    return {
      // fileList1: [],
    }
  },
  methods:{
    // 删除图片
    deletePic(event) {
      this[`fileList${event.name}`].splice(event.index, 1)
      // console.log(this.fileList1)
      // this.$emit('upload', this.fileList1)
    },
    // 新增图片
    async afterRead(event) {
      // 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
      let lists = [].concat(event.file)
      let fileListLen = this[`fileList${event.name}`].length
      lists.map((item) => {
        this[`fileList${event.name}`].push({
          ...item,
          status: 'uploading',
          message: '上传中'
        })
      })
      for (let i = 0; i < lists.length; i++) {
        const result = await this.uploadFilePromise(lists[i].url)
        // console.log(result)
        let item = this[`fileList${event.name}`][fileListLen]
        this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
          status: 'success',
          message: '',
          // url: result
          url: config.baseUrl + result.full_name,
          id: result.id
        }))
        fileListLen++
        // console.log(this.fileList1)
        // this.$emit('upload', this.fileList1)
      }
    },
    uploadFilePromise(url) {
      return new Promise((resolve, reject) => {

        // let request_url = `${conf.baseUrl}${url}`
        //
        // if (conf.dev) {
        //   request_url = request_url.replace('/api', '/supplier')
        // }
        //
        let request_url = 'api'
        if (config.dev) {
          request_url = 'supplier'
        }

        const target = `${config.baseUrl}/${request_url}/upload/upload?max-width=750`
        const token = this.$store.getters['User/token']
        let a = uni.uploadFile({
          url: target,
          filePath: url,
          name: 'file',
          header: {
            [config.token_name]: token,
            // 'content-type': 'application/json;charset=utf8mb4',
          },
          formData: {
          },
          success: (res) => {
            let { data } = res
            data = JSON.parse(data)
            // console.log(data)
            setTimeout(() => {
              resolve(data.data)
            }, 10)
          }
        });
      })
    },
  }

}
</script>

<style scoped>

</style>
